package com.amoyt.project.controller.visitor;

import com.amoyt.project.domain.dto.VisitorLoginDto;
import com.amoyt.project.domain.dto.update.VisitorUpdateDto;
import com.amoyt.project.domain.res.Result;
import com.amoyt.project.regex.RegexPatterns;
import com.amoyt.project.service.security.UserService;
import com.amoyt.project.service.visitor.VisitorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @Date: 2025/8/8
 * @Time: 16:12
 * @Author: TQ_QT
 * @Description: 访客登录接口
 */
@RestController
@RequestMapping("/visitor")
@Slf4j
@Tag(name = "访客相关接口",description = "访客登录接口")
public class VisitorController {

    @Autowired
    private VisitorService visitorService;

    @GetMapping("/code")
    @Operation(summary = "获取验证码",method = "GET")
    public Result getCode(String phone) {
        if (phone == null || !phone.matches(RegexPatterns.PHONE_REGEX)){
            return Result.fail("请输入正确的手机号");
        }
        return visitorService.getPhoneCode(phone);
    }

    /**
     * 根据手机号注册 （默认用手机号做用户名）
     * @param visitorLoginDto
     * @return
     */
    @PostMapping("/register")
    @Operation(summary = "访客注册",method = "POST")
    public Result register(@RequestBody VisitorLoginDto visitorLoginDto) {
        if (visitorLoginDto.getPhone() == null || !visitorLoginDto.getPhone().matches(RegexPatterns.PHONE_REGEX)){
            return Result.fail("请输入正确的手机号");
        }
        if (visitorLoginDto.getCode() == null || !visitorLoginDto.getCode().matches(RegexPatterns.VERIFY_CODE_REGEX)){
            return Result.fail("请输入正确的验证码");
        }

        return visitorService.register(visitorLoginDto);
    }

    /**
     * 校验验证码是否正确
     * @param phone
     * @param code
     * @return
     */
    @GetMapping("/verify")
    @Operation(summary = "校验验证号码",method = "GET")
    public Result verify(@RequestParam("phone") String phone, @RequestParam("code") String code){
        if (phone == null || !phone.matches(RegexPatterns.PHONE_REGEX)){
            return Result.fail("请输入正确的手机号");
        }
        if (code == null || !code.matches(RegexPatterns.VERIFY_CODE_REGEX)){
            return Result.fail("请输入正确的验证码");
        }
        return visitorService.verify(phone, code);
    }

    /**
     * 访客登录
     * @return
     */
    @PostMapping("/login")
    @Operation(summary = "访客登录",method = "POST")
    public Result login(@RequestBody VisitorLoginDto visitorLoginDto) {
        if (visitorLoginDto.getUsername() == null || visitorLoginDto.getUsername().equals("")){
            return Result.fail("账号不能为空");
        }
        if (visitorLoginDto.getPassword() == null || visitorLoginDto.getPassword().equals("")){
            return Result.fail("密码不能为空");
        }
        return visitorService.login(visitorLoginDto);
    }

    @GetMapping("/logout")
    @Operation(summary = "登出",method = "GET")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasRole('user')")
    public Result logout() {
        return visitorService.logout();
    }

    /**
     * 更新用户个人信息
     * @param visitorUpdateDto
     * @return
     */
    @PutMapping("/update")
    @Operation(summary = "更新用户信息",method = "PUT")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    public Result update(@RequestBody VisitorUpdateDto visitorUpdateDto) {
        if (visitorUpdateDto == null || visitorUpdateDto.getUserId() == null || visitorUpdateDto.getUserId().equals("")){
            return Result.fail("用户ID不能为空");
        }
        return visitorService.update(visitorUpdateDto);
    }

    /**
     * 获取当前用户信息
     * @return
     */
    @GetMapping("/info")
    @Parameter(name = "token",description = "token",required = true,in = ParameterIn.HEADER)
    @Operation(summary = "获取当前用户信息",method = "GET")
    public Result getInfo() {
        return visitorService.getOwnInfo();
    }

}
